import CryptoJS from "crypto-js";
import service from "../api/service";
import { ROBOT_ACCESS_TOKEN, ROBOT_SYSTERM_ID } from "../constant";

let baseUrl = "";
let schoolId = localStorage.getItem("schoolId");

export const setCommonBaseUrl = (addr) => {
  baseUrl = addr;
};

export const setCommonConfig = (config) => {
  schoolId = config.schoolId;
};

export const getClass = async () => {
  return service.get(baseUrl + `/BaseApi/UserMgr/UserInfoMgr/GetClass`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
    SchoolID: schoolId,
  });
};

export const getSubject = async () => {
  return service.get(
    baseUrl + `/BaseApi/UserMgr/TeachInfoMgr/GetSchoolSubjectInfo`,
    {
      appid: ROBOT_SYSTERM_ID,
      access_token: ROBOT_ACCESS_TOKEN,
      SchoolID: schoolId,
    }
  );
};

export const getGrade = async () => {
  return service.get(baseUrl + `/BaseApi/UserMgr/UserInfoMgr/GetGrade`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
    SchoolID: schoolId,
  });
};

export const getStudent = async () => {
  return service.get(baseUrl + `/BaseApi/UserMgr/UserInfoMgr/GetStudent`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
    schoolID: schoolId,
  });
};

export const getTeacher = async () => {
  return service.get(baseUrl + `/BaseApi/UserMgr/UserInfoMgr/GetTeacher`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
    schoolID: schoolId,
  });
};

export const getRoom = async (url, header) => {
  return service.get(
    url + `/roomInfo/getRoomInfos`,
    {
      buildingId: 0,
      functionType: 0,
    },
    header
  );
};

export const getBuilding = async (url, header) => {
  return service.get(
    url + `/buildingInfo/getBuildingByFlag`,
    {
      flag: 1,
      schoolId: schoolId,
    },
    header
  );
};

export const getBuilding_buildingAndArea = async (url, header) => {
  return service.get(
    url + `/buildingInfo/getBuildingByFlag`,
    {
      flag: -1,
      schoolId: schoolId,
    },
    header
  );
};

export const getArea = async (url, header) => {
  return service.get(
    url + `/buildingInfo/getBuildingByFlag`,
    {
      flag: 0,
      schoolId: schoolId,
    },
    header
  );
};

export const getCanteen = async (url) => {
  return service.get(
    url + `/support/api/canteenInfo`,
    {
      sysId: "E82",
      schoolId: schoolId,
    },
    {
      schoolId,
    }
  );
};

export const getEquipment = async (url, header) => {
  return service.get(
    url + `/equipment/listEquipment`,
    {
      category: 0,
      type: 0,
      status: 0,
      buildingId: -1,
      roomId: -1,
      flag: 1,
      pageIndex: 1,
      pageSize: 1000,
      schoolId: schoolId,
    },
    header
  );
};

export const getVistorList = async (url, header) => {
  return service.get(
    url + `outside/todayVisitorInfo`,
    {
      schoolId: schoolId,
    },
    header
  );
};

export const getSubSystemsMainServerBySubjectID = async (sysIDs) => {
  return service.get(
    baseUrl + `/BaseApi/Global/GetSubSystemsMainServerBySubjectID`,
    {
      appid: ROBOT_SYSTERM_ID,
      access_token: ROBOT_ACCESS_TOKEN,
      sysIDs: sysIDs,
      schoolId,
    }
  );
};

export const getIdentityBySchoolID = async () => {
  return service.get(baseUrl + `/UserMgr/PowerMgr/GetIdentityBySchoolID`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
    SchoolID: schoolId,
  });
};

export const getAllSystemInfo = async () => {
  return service.get(baseUrl + `/BaseApi/Global/GetSubSystemsInfoBySchool`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
    schoolId: schoolId,
  });
};

export const getCurSchoolInfo = async () => {
  let result = null;
  await service
    .get(baseUrl + `/BaseApi/SysMgr/Setting/GetSchoolsInfo`, {
      appid: ROBOT_SYSTERM_ID,
      access_token: ROBOT_ACCESS_TOKEN,
      schoolIDs: schoolId,
    })
    .then((res) => {
      result = res;
    })
    .catch((err) => {});

  if (!result) {
    return;
  }

  const { Data } = result;
  if (Data?.length && Data.length > 0) {
    return Data[0];
  }

  return null;
};

export const getAllSchoolInfo = async () => {
  return service.get(baseUrl + `/BaseApi/SysMgr/Setting/GetSchoolsInfo`, {
    appid: ROBOT_SYSTERM_ID,
    access_token: ROBOT_ACCESS_TOKEN,
  });
};

export const getIdentityTypeByUserID = async (userId, token) => {
  return service.get(
    baseUrl + `/UserMgr/PowerMgr/GetIdentityTypeByUserID`,
    {
      UserID: userId,
    },
    {
      Authorization: token,
    }
  );
};

export const getGeneralList = async (url, msg, userType, token) => {
  return service.get(
    url + `openApi/260/searchServ`,
    {
      keyword: msg,
      schoolId,
      userType,
      identityCodeList: "0",
    },
    {
      Authorization: token,
    }
  );
};

/**
 * 获取锁控信息
 * @returns
 */
export const getSubSystemLockerInfoByID = async () => {
  const requestTime = new Date()
    .toLocaleString("zh-CN", { hour12: false })
    .replace(/\//g, "-");

  return service.get(
    baseUrl + `/BaseApi/LockerMgr/GetSubSystemLockerInfoByID`,
    {
      appid: ROBOT_SYSTERM_ID,
      access_token: ROBOT_ACCESS_TOKEN,
      lockerID: 633,
      schoolID: schoolId,
      requestTime,
      secCode: CryptoJS.MD5(633 + requestTime)
        .toString()
        .split("")
        .reverse()
        .join(""),
    }
  );
};

/**
 * 根据用户id获取所有的行政班（中职、大学、中小学）
 * @param {*} userId 用户id
 * @returns
 */
export const getAdministrativeClassInfo = async (userId, version) => {
  if (version == "2") {
    return service.get(
      baseUrl + `/BaseApi/UserMgr/UserInfoMgr/GetClassByUser`,
      {
        appid: ROBOT_SYSTERM_ID,
        access_token: ROBOT_ACCESS_TOKEN,
        teacherID: userId,
      }
    );
  } else {
    return service.get(
      baseUrl + `/BaseApi/UserMgr/UserInfoMgr/GetClassByUser_Univ`,
      {
        appid: ROBOT_SYSTERM_ID,
        access_token: ROBOT_ACCESS_TOKEN,
        teacherID: userId,
      }
    );
  }
};

/**
 * 根据用户id获取所有的教学班
 * @param {*} userId 用户id
 * @returns
 */
export const getCourseClassInfo = async (userId) => {
  return service.get(
    baseUrl + `/BaseApi/UserMgr/TeachInfoMgr/GetCourseClassInfo`,
    {
      appid: ROBOT_SYSTERM_ID,
      access_token: ROBOT_ACCESS_TOKEN,
      schoolID: schoolId,
      userID: userId,
    }
  );
};

/**
 * 根据教学班id获取所有的学生信息
 * @param {*} courseClassID 教学班id
 * @returns
 */
export const getCourseStudents = async (courseClassID) => {
  return service.get(
    baseUrl + `/BaseApi/UserMgr/TeachInfoMgr/GetCourseStudents`,
    {
      appid: ROBOT_SYSTERM_ID,
      access_token: ROBOT_ACCESS_TOKEN,
      schoolID: schoolId,
      courseClassID,
    }
  );
};
